/**
 * 我的发布
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/6
 */
var self = memberInfo = '';

var index = 1;

document.addEventListener('plusready' , ready ,  false);

function ready(){
	self = plus.webview.currentWebview(); 
	memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);
 	//加载数据
 	$.ajax({  
		type:"post",
		url: apiRoot + '/Home/Demand/myDemand/' ,
		data:{
			
			uid : memberInfo.aid
		}, 
		success : function(data){ 
			plus.nativeUI.closeWaiting();
			plus.storage.setItem('fangan' , data);
			data = $.parseJSON(data);
			if(data != ''){   
				$.each(data , function(k , v) { 
					var html = '<div class="background-w table-view table-view-cell margin-bottom-5 me-fb-list"><li><span class="mui-ellipsis-2 line-h-23 font-16 title'+v.id+'">'+v.title+'</span></li>';
						html += '<li class="color-c font-13 margin-top-5"><span>'+v.addtime+'</span><span class="mui-pull-right"><button class="mlr me-fb-x details" data-id="'+v.id+'" data-proposal="'+v.proposal+'" data-demand="'+v.demand+'">详情</button><button class="me-fb-fa" onclick="programme(\''+v.demand+'\' , \''+v.id+'\');">'+v.recommend.length+'个方案</button></span></li></div>';
					$('.mui-content').append(html);
				}); 
				$('.details').unbind();
				
				$('.details').on('click' , function(){
					plus.nativeUI.showWaiting();
					var __this = $(this);
					var me_fb_inf = plus.webview.create('me-fb-inf.html', 'me_fb_inf', {}, {
						aid : __this.attr('data-id'),
						page : self.id, 
						content : __this.attr('data-demand'),
						proposal : __this.attr('data-proposal')
					});
					me_fb_inf.addEventListener('loaded', function (){
						me_fb_inf.show('pop-in');
						plus.nativeUI.closeWaiting();
					})	
					
				})
				
			}else{
				toast('没有数据'); 
			}
			
		}, 
		
		error : function(e){ 
			
			plus.nativeUI.closeWaiting();
			
			toast('连接超时,请检查网络');
			
			alert(JSON.stringify(e))
			
		} 
	});
	
 	
	//发布需求
	$('.add_demand').on('click' , function(){ 
		plus.nativeUI.showWaiting('跳转中...');
		var me_fb = plus.webview.create('me-fb.html', 'me_fb', {}, {
			page : self.id,
			uid : memberInfo.aid
		});
		me_fb.addEventListener('loaded', function (){
			me_fb.show('pop-in');
			plus.nativeUI.closeWaiting();
		})
	})
 	 
}



window.addEventListener('add_demand',function(event){
	var html = '<div class="background-w table-view table-view-cell margin-bottom-5 me-fb-list"><li><span class="mui-ellipsis-2 line-h-23 font-16" title'+event.detail.id+'">'+event.detail.title+'</span></li>';
	html += '<li class="color-c font-13 margin-top-5"><span>刚刚</span><span class="mui-pull-right"><button class="mlr me-fb-x details" data-id="'+event.detail.id+'" data-proposal="'+event.detail.proposal+'" data-demand="'+event.detail.demand+'">详情</button><button class="me-fb-fa" onclick="programme(\''+event.detail.demand+'\' , \''+event.detail.id+'\');">0个方案</button></span></li></div>';
	$('.mui-content').prepend(html);
	
	$('.details').unbind();
	$('.details').on('click' , function(){
		plus.nativeUI.showWaiting();
		var __this = $(this);
		var me_fb_inf = plus.webview.create('me-fb-inf.html', 'me_fb_inf', {}, {
			aid : __this.attr('data-id'),
			page : self.id, 
			content : __this.attr('data-demand'),
			proposal : __this.attr('data-proposal')
		});
		me_fb_inf.addEventListener('loaded', function (){
			me_fb_inf.show('pop-in');
			plus.nativeUI.closeWaiting();
		})	
		
	})
	
	
});

window.addEventListener('update_demand',function(event){
	$('.title'+event.detail.id).text(event.detail.title);
});

///**
// * 查看详情 
// * @param {Object} content
// * @param {Object} proposal 
// */ 
//function details(id , content , proposal){
//	
//}

/**
 * 查看推荐方案
 * @param content
 * @param id
 */
function programme(content , id){
	var me_fb_inf_fa = plus.webview.create('me-fb-inf-fa.html', 'me_fb_inf_fa', {}, {
		content : content,
		aid : id
	});
	me_fb_inf_fa.addEventListener('loaded', function (){
		me_fb_inf_fa.show('pop-in');
	})	
}
